import {Injectable} from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {FormUtil} from "../../../util/form-util";
import {DataSource} from "./data-source";
import {Observable} from "rxjs/Observable";
import {MsgResult} from "../../../models/msg-result";
import {TemplateAPI} from "../template-constants";

@Injectable()
export class DataSourceService {

  constructor(private request: RequestUtil,
              private formUtil: FormUtil) {
  }

  //添加实例
  addDataSource(dataSource: DataSource): Observable<MsgResult> {
    let params = "name=" + dataSource.name;
    params += "&type=" + dataSource.type;
    params += "&dataFromSrc=" + dataSource.dataFromSrc;
    params += "&orderBy=" + dataSource.orderBy;
    params += "&description=" + dataSource.description;
    params += "&url=" + dataSource.url;
    params += "&isUse=" + dataSource.isUse;
    return this.request.post(TemplateAPI.insertDataSource, params);
  }

  //删除实例
  deleteDataSource(id: string): Observable<MsgResult> {
    return this.request.delete(TemplateAPI.deleteDataSource + "&id=" + id);
  }

  //批量删除
  deleteDataSources(ids: Array<String>): Observable<MsgResult> {
    let params = "idStrs=" + ids;
    return this.request.post(TemplateAPI.delDataSources, params);
  }

  //根据id获得规则
  getDataSourceById(id: string): Observable<MsgResult> {
    return this.request.get(TemplateAPI.getDataSourceById + "&id=" + id);
  }

  //修改实例
  updateDataSource(dataSource: DataSource): Observable<MsgResult> {
    let params = "name=" + dataSource.name;
    params += "&id=" + dataSource.id;
    params += "&type=" + dataSource.type;
    params += "&dataFromSrc=" + dataSource.dataFromSrc;
    params += "&orderBy=" + dataSource.orderBy;
    params += "&description=" + dataSource.description;
    params += "&url=" + dataSource.url;
    params += "&isUse=" + dataSource.isUse;
    return this.request.put(TemplateAPI.updateDataSource, params);
  }
}
